package com.dpmc.service;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.common.bo.Node;
import com.common.util.ObjectUtils;
import com.common.util.ObjectXmlUtils;
import com.common.util.impl.ObjectXmlUtilsAttributeImpl;
import com.core.service.AppBaseService;
import com.dpmc.entity.*;

/**
 * 商户管理
 */
@Stateless
public class SVC_SHGL extends AppBaseService implements SVC_SHGLRemote,
		SVC_SHGLLocal {
	@Resource(name = "JmsConnectionFactory")
	private ConnectionFactory connectionFactory;

	@Override
	public Map ADD_LXDY(Map param) {
		TB_SHGL_LXDY bo = ObjectUtils.mapToJavaBean(param, TB_SHGL_LXDY.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_SHGL_LXDY.STATUS_ACTIVITY);
		getJpaDao().save(bo); 
		

		List<Map> LIST_TB_SPGL_SPFL_SHLX = ObjectUtils.getMapValue(param,
				"LIST_TB_SPGL_SPFL_SHLX", List.class);
		if (LIST_TB_SPGL_SPFL_SHLX != null) {
			for (Map item : LIST_TB_SPGL_SPFL_SHLX) {
				TB_SPGL_SPFL_SHLX r_bo = ObjectUtils.mapToJavaBean(item,
						TB_SPGL_SPFL_SHLX.class);
				r_bo.setID(r_bo.uuid());
				r_bo.setSHLX_ID(bo.getID());
				getJpaDao().save(r_bo);
			}
		}
		List<Map> LIST_TB_SHGL_LXDY_KZSX = ObjectUtils.getMapValue(param,
				"LIST_TB_SHGL_LXDY_KZSX", List.class);
		if (LIST_TB_SHGL_LXDY_KZSX != null) {
			for (Map item : LIST_TB_SHGL_LXDY_KZSX) {
				TB_SHGL_LXDY_KZSX r_bo = ObjectUtils.mapToJavaBean(item,
						TB_SHGL_LXDY_KZSX.class);
				r_bo.setID(r_bo.uuid());
				r_bo.setLXDYID(bo.getID());
				r_bo.setCREATE_TIME(getOpTime(param));
				r_bo.setOP_ITEM(getOpTime(param));
				r_bo.setOPID(getOpid(param));
				r_bo.setSTATUS(TB_SHGL_LXDY_KZSX.STATUS_ACTIVITY);
				getJpaDao().save(r_bo);
			}
		}

		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_LXDY(Map param) {

		TB_SHGL_LXDY dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_LXDY.class);
		TB_SHGL_LXDY po = getJpaDao().find(TB_SHGL_LXDY.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);

		List<Map> LIST_TB_SPGL_SPFL_SHLX = ObjectUtils.getMapValue(param,
				"LIST_TB_SPGL_SPFL_SHLX", List.class);
		if (LIST_TB_SPGL_SPFL_SHLX != null) {
			StringBuffer hql = new StringBuffer(
					"delete from TB_SPGL_SPFL_SHLX o")
					.append(" where o.SHLX_ID='").append(po.getID())
					.append("'");

			doExecuteUpdate(hql.toString());

			for (Map item : LIST_TB_SPGL_SPFL_SHLX) {
				TB_SPGL_SPFL_SHLX r_bo = ObjectUtils.mapToJavaBean(item,
						TB_SPGL_SPFL_SHLX.class);
				r_bo.setID(r_bo.uuid());
				r_bo.setSHLX_ID(po.getID());
				getJpaDao().save(r_bo);
			}
		}

		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_LXDY(Map param) {
		TB_SHGL_LXDY dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_LXDY.class);
		TB_SHGL_LXDY po = getJpaDao().find(TB_SHGL_LXDY.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map ADD_SHJBXX(Map param) {
		TB_SHGL_SHJBXX shjbxx = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHJBXX.class);
		shjbxx.setID(shjbxx.uuid());
		shjbxx.setCREATE_TIME(getOpTime(param));
		shjbxx.setOP_ITEM(shjbxx.getCREATE_TIME());
		shjbxx.setOPID(getOpid(param));
		shjbxx.setSTATUS(TB_SHGL_SHJBXX.STATUS_NEW);
		getJpaDao().save(shjbxx);

		// 分类关系
		List list_TB_SHGL_SHFL = ObjectUtils.getMapValue(param,
				"list_TB_SHGL_SHFL", List.class);
		if (list_TB_SHGL_SHFL != null) {
			for (Object item : list_TB_SHGL_SHFL) {
				TB_SHGL_SHFL shfl = ObjectUtils.mapToJavaBean((Map) item,
						TB_SHGL_SHFL.class);
				shfl.setID(shfl.uuid());
				shfl.setSHID(shjbxx.getID());
				getJpaDao().save(shfl);
			}
		}
		// 商户财务账户
		Map cwzh = new HashMap();
		cwzh.put("ZHLX", "TB_SHGL_SHJBXX");
		cwzh.put("GLID", shjbxx.getID());
		copySysParam(param, cwzh);
		getServiceInterface().doService("SVC_CWGL", "ADD_ZH", cwzh);

		String wfCode = "wf_shxz";
		String opid = getOpid(param);
		Date optime = getOpTime(param);
		String bo_id = shjbxx.getID();
		int bo_STATUS = shjbxx.getSTATUS();
		String approveResult = "商户录入";
		String approveMemo = null;
		getServiceInterface().doService("SVC_WORKFLOW", "DO_START_PROCESS",
				wfCode, opid, optime, bo_id, bo_STATUS, approveResult,
				approveMemo);
		
		
		Map USER=(Map)param.get("OBJECT_SYS_USER");
		if(USER!=null){
			copySysParam(param, USER);
			USER.put("INFO_ID", shjbxx.getID()); 
			USER.put("INFO_TYPE", "TB_SHGL_SHJBXX");   
			USER.put("INFO_NAME", shjbxx.getSJMC());   
			getServiceInterface().doService("RMServiceBean", "addSysUser", USER); 
		}  

		Map retMap = ObjectUtils.javaBeanToMap(shjbxx); 
		return retMap;
	}

	@Override
	public Map UPD_SHJBXX(Map param) {
 
		TB_SHGL_SHJBXX shjbxx = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHJBXX.class);
		shjbxx.setOP_ITEM(getOpTime(param));
		shjbxx.setOPID(getOpid(param));

		TB_SHGL_SHJBXX shjbxx_po = getJpaDao().find(TB_SHGL_SHJBXX.class,
				shjbxx.getID());
		ObjectUtils.copyJavaBeanToBean(shjbxx, shjbxx_po, "CREATE_TIME");
		getJpaDao().update(shjbxx_po);

		List list_TB_SHGL_SHFL = ObjectUtils.getMapValue(param,
				"list_TB_SHGL_SHFL", List.class);
		if (list_TB_SHGL_SHFL != null) {
			StringBuffer hql = new StringBuffer(
					"delete from TB_SHGL_SHFL where SHID='").append(
					shjbxx_po.getID()).append("'");
			doExecuteUpdate(hql.toString());

			for (Object item : list_TB_SHGL_SHFL) {
				TB_SHGL_SHFL shfl = ObjectUtils.mapToJavaBean((Map) item,
						TB_SHGL_SHFL.class);
				shfl.setID(shfl.uuid());
				shfl.setSHID(shjbxx.getID());
				getJpaDao().save(shfl);
			}
		}
		
		Map USER=(Map)param.get("OBJECT_SYS_USER");
		if(USER!=null){
			copySysParam(param, USER);
			USER.put("INFO_ID", shjbxx.getID()); 
			USER.put("INFO_TYPE", "TB_SHGL_SHJBXX");   
			USER.put("INFO_NAME", shjbxx.getSJMC());   
			if(USER.get("ID")!=null){
				getServiceInterface().doService("RMServiceBean", "updateSysUser", USER); 
			}else{
				getServiceInterface().doService("RMServiceBean", "addSysUser", USER); 
			} 
		}  
		param.put("ID", shjbxx_po.getID());
		SAVE_KZSX(param);

		Map retMap = ObjectUtils.javaBeanToMap(shjbxx_po);
		return retMap;
	}

	@Override
	public void DEL_SHJBXX(Map param) {
		TB_SHGL_SHJBXX dto = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHJBXX.class);
		TB_SHGL_SHJBXX po = getJpaDao().find(TB_SHGL_SHJBXX.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_SHJBXX.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map SAVE_RZXX(Map param) {
		TB_SHGL_RZXX vo = ObjectUtils.mapToJavaBean(param, TB_SHGL_RZXX.class);
		if (vo.getID() == null) {
			vo.setID(vo.uuid());
			getJpaDao().save(vo);
		} else {
			getJpaDao().update(vo);
		}
		Map retMap = ObjectUtils.javaBeanToMap(vo);
		return retMap;
	}

	@Override
	public Map SAVE_XYXX(Map param) {
		TB_SHGL_SHXYXX vo = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHXYXX.class);
		if (vo.getID() == null) {
			vo.setCREATE_TIME(getOpTime(param));
			vo.setOP_ITEM(getOpTime(param));
			vo.setOPID(getOpid(param));
			vo.setID(vo.uuid());
			getJpaDao().save(vo);
		} else {
			TB_SHGL_SHXYXX po = getJpaDao().find(TB_SHGL_SHXYXX.class,
					vo.getID());
			vo.setOP_ITEM(getOpTime(param));
			vo.setOPID(getOpid(param));
			ObjectUtils.copyJavaBeanToBean(vo, po, "CREATE_TIME");

			getJpaDao().update(po);
		}
		Map retMap = ObjectUtils.javaBeanToMap(vo);
		return retMap;
	}

	@Override
	public Map SAVE_YHZH(Map param) {
		log(param.toString());
		TB_YHZH vo = ObjectUtils.mapToJavaBean(param, TB_YHZH.class);
		vo.setOP_ITEM(getOpTime(param));
		vo.setOPID(getOpid(param));
		if (vo.getID() == null) {
			vo.setID(vo.uuid());
			vo.setGLLX("TB_SHGL_SHJBXX");
			vo.setCREATE_TIME(getOpTime(param));
			getJpaDao().save(vo);
		} else {
			TB_YHZH po = getJpaDao().find(TB_YHZH.class, vo.getID());
			ObjectUtils.copyJavaBeanToBean(vo, po, "CREATE_TIME", "GLLX",
					"GLID");
			getJpaDao().update(po);
		}
		Map retMap = ObjectUtils.javaBeanToMap(vo);
		return retMap;
	}

	@Override
	public String GET_SPFL_SHLX_TREE(String lx_id) {
		StringBuffer ql = new StringBuffer(
				"SELECT new com.common.bo.Node(sp_fl.ID,sp_fl.FLMC,'"
						+ Node.STATE_CHECKED + "')")
				.append(" FROM TB_SPGL_SPFL_SHLX sp_sh_fl,TB_SPGL_FLXX sp_fl")
				.append(" WHERE sp_sh_fl.SPFL_ID=sp_fl.ID")
				.append(" AND sp_sh_fl.SHLX_ID='").append(lx_id).append("'");
		List result = getResultList(ql.toString());
		Iterator iterator = result.iterator();
		Set<String> ids = new HashSet<String>();
		ids.add("");
		while (iterator.hasNext()) {
			Node r = (Node) iterator.next();
			ids.add(r.getId());
		}
		ql = new StringBuffer("select new com.common.bo.Node(r.ID,r.FLMC,'"
				+ Node.STATE_UNCHECKED + "')")
				.append(" from TB_SPGL_FLXX r where r.ID not in(:ids)");
		HashMap param = new HashMap();
		param.put("ids", ids);

		result.addAll(getResultList(ql.toString(), param));

		Node root = new Node();
		root.setChildren(result);

		ObjectXmlUtils utils = new ObjectXmlUtilsAttributeImpl();
		if (root != null) {
			return utils.objectToXml(root);
		}
		return null;
	}

	@Override
	public Map ADD_ZBJT(Map param) {
		TB_SHGL_ZBJT bo = ObjectUtils.mapToJavaBean(param, TB_SHGL_ZBJT.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_SHGL_LXDY.STATUS_ACTIVITY);
		getJpaDao().save(bo);
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_ZBJT(Map param) {
		TB_SHGL_ZBJT dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_ZBJT.class);
		TB_SHGL_ZBJT po = getJpaDao().find(TB_SHGL_ZBJT.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_ZBJT(Map param) {
		TB_SHGL_ZBJT dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_ZBJT.class);
		TB_SHGL_ZBJT po = getJpaDao().find(TB_SHGL_ZBJT.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map ADD_LXDY_KZSX(Map param) {
		TB_SHGL_LXDY_KZSX bo = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_LXDY_KZSX.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_SHGL_LXDY_KZSX.STATUS_ACTIVITY);
		getJpaDao().save(bo);

		List list_TB_SHGL_LXDY_KZSXZFW = (List) param
				.get("list_TB_SHGL_LXDY_KZSXZFW");
		if (list_TB_SHGL_LXDY_KZSXZFW != null) {
			for (Object item : list_TB_SHGL_LXDY_KZSXZFW) {
				TB_SHGL_LXDY_KZSXZFW z = ObjectUtils.mapToJavaBean((Map) item,
						TB_SHGL_LXDY_KZSXZFW.class);
				z.setCREATE_TIME(getOpTime(param));
				z.setID(z.uuid());
				z.setOP_ITEM(getOpTime(param));
				z.setOPID(getOpid(param));
				z.setKZSXID(bo.getID());
				z.setLXDYID(bo.getLXDYID());

				getJpaDao().save(z);
			}
		}

		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_LXDY_KZSX(Map param) {
		TB_SHGL_LXDY_KZSX dto = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_LXDY_KZSX.class);
		TB_SHGL_LXDY_KZSX po = getJpaDao().find(TB_SHGL_LXDY_KZSX.class,
				dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);

		List<TB_SHGL_LXDY_KZSXZFW> tempList = (List<TB_SHGL_LXDY_KZSXZFW>) getResultList("select o from TB_SHGL_LXDY_KZSXZFW o where KZSXID='"
				+ po.getID() + "' and LXDYID='" + po.getLXDYID() + "'");
		HashMap<String, TB_SHGL_LXDY_KZSXZFW> tmpMap = new HashMap<String, TB_SHGL_LXDY_KZSXZFW>();
		for (TB_SHGL_LXDY_KZSXZFW item : tempList) {
			tmpMap.put(item.getID(), item);
		}

		List list_TB_SHGL_LXDY_KZSXZFW = (List) param
				.get("list_TB_SHGL_LXDY_KZSXZFW");
		if (list_TB_SHGL_LXDY_KZSXZFW != null) {
			for (Object item : list_TB_SHGL_LXDY_KZSXZFW) {
				TB_SHGL_LXDY_KZSXZFW z = ObjectUtils.mapToJavaBean((Map) item,
						TB_SHGL_LXDY_KZSXZFW.class);

				z.setOP_ITEM(getOpTime(param));
				z.setOPID(getOpid(param));
				z.setKZSXID(po.getID());
				z.setLXDYID(po.getLXDYID());

				if (z.getID() == null) {
					z.setCREATE_TIME(getOpTime(param));
					z.setID(z.uuid());
					getJpaDao().save(z);
				} else {
					TB_SHGL_LXDY_KZSXZFW z_po = tmpMap.get(z.getID());
					ObjectUtils.copyJavaBeanToBean(z, z_po, "CREATE_TIME");
					getJpaDao().update(z_po);
				}
			}
		}

		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_LXDY_KZSX(Map param) {
		TB_SHGL_LXDY_KZSX dto = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_LXDY_KZSX.class);
		TB_SHGL_LXDY_KZSX po = getJpaDao().find(TB_SHGL_LXDY_KZSX.class,
				dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map SAVE_KZSX(Map param) {

		List LIST_TB_SHGL_SHJBXX_KZSX = ObjectUtils.getMapValue(param,
				"LIST_TB_SHGL_SHJBXX_KZSX", List.class);

		if (LIST_TB_SHGL_SHJBXX_KZSX != null) {
			for (Object item : LIST_TB_SHGL_SHJBXX_KZSX) {
				List<Map> LIST_KZSX_DXSXZ = (List) ((Map) item)
						.get("LIST_KZSX_DXSXZ");
				TB_SHGL_SHJBXX_KZSX kzsx = ObjectUtils.mapToJavaBean(
						(Map) item, TB_SHGL_SHJBXX_KZSX.class);
				kzsx.setOP_ITEM(getOpTime(param));
				kzsx.setOPID(getOpid(param));
				String shid = (String) param.get("ID");
				if((kzsx.getSHID()==null||kzsx.getSHID().trim()=="")&&shid!=null&&shid.trim()!=""){
					kzsx.setSHID(shid);
				}
				if (kzsx.getID() == null) {
					//新增扩展属性
					if (LIST_KZSX_DXSXZ == null) {
						if (kzsx.getSXZ() != null && !kzsx.getSXZ().isEmpty()) {
							kzsx.setID(kzsx.uuid());
							kzsx.setCREATE_TIME(getOpTime(param));
							kzsx.setSTATUS(TB_SHGL_LXDY_KZSX.STATUS_ACTIVITY);
							getJpaDao().save(kzsx);
							((Map) item).put("ID", kzsx.getID());
						}
					} else {// 处理对象列表 
						kzsx.setID(kzsx.uuid());
						kzsx.setSXZ(kzsx.getID());
						kzsx.setCREATE_TIME(getOpTime(param));
						kzsx.setSTATUS(TB_SHGL_LXDY_KZSX.STATUS_ACTIVITY);
						getJpaDao().save(kzsx);
						((Map) item).put("ID", kzsx.getID());
						for (Map dx : LIST_KZSX_DXSXZ) {
							TB_SHGL_SHJBXX_KZSX_DXSXZ dxsxz = ObjectUtils
									.mapToJavaBean((Map) dx,
											TB_SHGL_SHJBXX_KZSX_DXSXZ.class);
							dxsxz.setDXLBID(kzsx.getID());
							dxsxz.setID(dxsxz.uuid());
							getJpaDao().save(dxsxz);
						}
					}

				} else {
					TB_SHGL_SHJBXX_KZSX po = getJpaDao().find(
							TB_SHGL_SHJBXX_KZSX.class, kzsx.getID());
					ObjectUtils.copyJavaBeanToBean(kzsx, po, "CREATE_TIME",
							"STATUS");
					getJpaDao().update(po);
					StringBuffer ids = new StringBuffer("''");
					if (LIST_KZSX_DXSXZ != null) {
						for (Map dx : LIST_KZSX_DXSXZ) {
							TB_SHGL_SHJBXX_KZSX_DXSXZ dxsxz = ObjectUtils
									.mapToJavaBean((Map) dx,
											TB_SHGL_SHJBXX_KZSX_DXSXZ.class);
							if (dxsxz.getID() != null) {
								TB_SHGL_SHJBXX_KZSX_DXSXZ dxsxz_po = getJpaDao()
										.find(TB_SHGL_SHJBXX_KZSX_DXSXZ.class,
												dxsxz.getID());
								dxsxz_po.setSXZ(dxsxz.getSXZ());
								getJpaDao().update(dxsxz_po);
							} else {
								dxsxz.setDXLBID(kzsx.getID());
								dxsxz.setID(dxsxz.uuid());
								getJpaDao().save(dxsxz);
							}
							ids.append(",'").append(dxsxz.getID()).append("'");
						}
						StringBuffer hql = new StringBuffer(
								"delete from TB_SHGL_SHJBXX_KZSX_DXSXZ where DXLBID='")
								.append(kzsx.getID()).append("'")
								.append(" and ID not in(").append(ids)
								.append(")");
						doExecuteUpdate(hql.toString());
					}

				}

			}
		}
		return param;
	}

	@Override
	public Map ADD_FJLX(Map param) {
		TB_SHGL_FJLX bo = ObjectUtils.mapToJavaBean(param, TB_SHGL_FJLX.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_SHGL_FJLX.STATUS_ACTIVITY);
		getJpaDao().save(bo);
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_FJLX(Map param) {
		TB_SHGL_FJLX dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_FJLX.class);
		TB_SHGL_FJLX po = getJpaDao().find(TB_SHGL_FJLX.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_FJLX(Map param) {
		TB_SHGL_FJLX dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_FJLX.class);
		TB_SHGL_FJLX po = getJpaDao().find(TB_SHGL_FJLX.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_FJLX.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map ADD_SCFJ(Map param) {

		TB_SHGL_SHFJ dto_SHFJ = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHFJ.class);
		dto_SHFJ.setID(dto_SHFJ.uuid());

		Map map_UPLOADFILE = ObjectUtils.getMapValue(param,
				"TB_UFM_UPLOADFILE", Map.class);
		copySysParam(param, map_UPLOADFILE);

		map_UPLOADFILE = (Map) getServiceInterface().doService(
				"SVC_UPLOADFILE", "ADD_BL_FILE", map_UPLOADFILE);

		dto_SHFJ.setFILE_ID(ObjectUtils.getMapValue(map_UPLOADFILE, "ID",
				String.class));
		dto_SHFJ.setFILE_NAME(ObjectUtils.getMapValue(map_UPLOADFILE,
				"FILE_NAME", String.class));
		dto_SHFJ.setFILE_SIZE(ObjectUtils.getMapValue(map_UPLOADFILE,
				"FILE_SIZE", Long.class));
		getServiceInterface().doService("SVC_UPLOADFILE", "buildLink",
				dto_SHFJ.getFILE_ID());

		if (dto_SHFJ.getLINK_ID() == null) {
			dto_SHFJ.setLINK_ID("");
		}
		if (dto_SHFJ.getLINK_TYPE() == null) {
			dto_SHFJ.setLINK_TYPE("");
		}
		getJpaDao().save(dto_SHFJ);
		Map ret = ObjectUtils.javaBeanToMap(dto_SHFJ);
		ret.put("TB_UFM_UPLOADFILE", map_UPLOADFILE);
		return ret;
	}

	@Override
	public Map UPD_SCFJ(Map param) {

		TB_SHGL_SHFJ dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_SHFJ.class);
		TB_SHGL_SHFJ po = getJpaDao().find(TB_SHGL_SHFJ.class, dto.getID());
		if (po.getLINK_ID() == null) {
			po.setLINK_ID("");
		}
		if (po.getLINK_TYPE() == null) {
			po.setLINK_TYPE("");
		}
		po.setXH(dto.getXH());
		po.setFJSM(dto.getFJSM());

		getJpaDao().update(po);
		return param;
	}

	@Override
	public void DEL_SCFJ(Map param) {
		TB_SHGL_SHFJ dto_SHFJ = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHFJ.class);
		TB_SHGL_SHFJ po = getJpaDao()
				.find(TB_SHGL_SHFJ.class, dto_SHFJ.getID());
		Map map_UPLOADFILE = new HashMap();
		map_UPLOADFILE.put("ID", po.getFILE_ID());
		getServiceInterface().doService("SVC_UPLOADFILE", "DEL_FILE",
				map_UPLOADFILE);
		getJpaDao().delete(TB_SHGL_SHFJ.class, po.getID());
	}

	@Override
	public Map ADD_POSAZSQB(Map param) {
		TB_SHGL_POSAZSQB bo = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_POSAZSQB.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_SHGL_FJLX.STATUS_ACTIVITY);
		getJpaDao().save(bo);

		List LIST_TB_SHGL_POS = ObjectUtils.getMapValue(param,
				"LIST_TB_SHGL_POS", List.class);

		if (LIST_TB_SHGL_POS != null) {
			for (Object item : LIST_TB_SHGL_POS) {
				TB_SHGL_POS pos = ObjectUtils.mapToJavaBean((Map) item,
						TB_SHGL_POS.class);
				if (pos.getID() == null) {
					pos.setID(pos.uuid());
				}
				pos.setCREATE_TIME(getOpTime(param));
				pos.setOP_ITEM(getOpTime(param));
				pos.setOPID(getOpid(param));
				pos.setAZSQB_ID(bo.getID());
				pos.setSHID(bo.getSHID());
				getJpaDao().save(pos);
			}
		}
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_POSAZSQB(Map param) {
		TB_SHGL_POSAZSQB dto = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_POSAZSQB.class);
		TB_SHGL_POSAZSQB po = getJpaDao().find(TB_SHGL_POSAZSQB.class,
				dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_POSAZSQB(Map param) {
		TB_SHGL_POSAZSQB dto_SHFJ = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_POSAZSQB.class);
		TB_SHGL_POSAZSQB po = getJpaDao().find(TB_SHGL_POSAZSQB.class,
				dto_SHFJ.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_FJLX.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map ADD_POS(Map param) {
		TB_SHGL_POS bo = ObjectUtils.mapToJavaBean(param, TB_SHGL_POS.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_SHGL_POS.STATUS_ACTIVITY);
		getJpaDao().save(bo);
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_POS(Map param) {
		TB_SHGL_POS dto = ObjectUtils.mapToJavaBean(param, TB_SHGL_POS.class);
		TB_SHGL_POS po = getJpaDao().find(TB_SHGL_POS.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_POS(Map param) {
		TB_SHGL_POS dto_SHFJ = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_POS.class);
		TB_SHGL_POS po = getJpaDao().find(TB_SHGL_POS.class, dto_SHFJ.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_FJLX.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public void UPD_SHJBXX_STATUS(Map param) {

		TB_SHGL_SHJBXX shjbxx = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHJBXX.class);

		TB_SHGL_SHJBXX shjbxx_po = getJpaDao().find(TB_SHGL_SHJBXX.class,
				shjbxx.getID());
		shjbxx_po.setSTATUS(shjbxx.getSTATUS());
		shjbxx_po.setOP_ITEM(getOpTime(param));
		shjbxx_po.setOPID(getOpid(param));
		getJpaDao().update(shjbxx_po);

		String taskId = (String) param.get("TASK_ID");
		String opid = getOpid(param);
		Date optime = getOpTime(param);
		String bo_id = null;
		int bo_STATUS = 0;
		String next_bo_id = shjbxx.getID();
		int next_bo_STATUS = shjbxx.getSTATUS();
		String approveResult = (String) param.get("TO_TASK_NAME");
		String approveMemo = (String) param.get("TASK_INFO");
		getServiceInterface().doService("SVC_WORKFLOW", "DO_TASK_SUBMIT",
				taskId, opid, optime, bo_id, bo_STATUS, next_bo_id,
				next_bo_STATUS, approveResult, approveMemo, null);

	}

	@Override
	public void RELEASE_INFO(Map param) {

		try {
			Connection connection = connectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(true, 0);
			Destination destination = session.createQueue("ReleaseShxxQueue");
			MessageProducer producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.PERSISTENT);

			/** 锁定商户信息，避免重复提交 ***/
			HashMap lockParam = new HashMap();
			lockParam.put("ID", param.get("ID"));
			TB_SHGL_SHJBXX shjbxx_po = getJpaDao().find(TB_SHGL_SHJBXX.class,
					(String) param.get("ID"));
			lockParam.put("STATUS", -(shjbxx_po.getSTATUS() + 10));
			copySysParam(param, lockParam);
			UPD_SHJBXX_STATUS_NO_WF(lockParam);

			HashMap fbdl = new HashMap();
			copySysParam(param, fbdl);
			fbdl.put("SHID", shjbxx_po.getID());

			ADD_XXFBDL(fbdl);

			TextMessage message = session.createTextMessage(ObjectUtils
					.objectToJson(param));
			producer.send(message);
			connection.close();

		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void UPD_SHJBXX_STATUS_NO_WF(Map param) {
		TB_SHGL_SHJBXX shjbxx = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_SHJBXX.class);
		TB_SHGL_SHJBXX shjbxx_po = getJpaDao().find(TB_SHGL_SHJBXX.class,
				shjbxx.getID());
		if (shjbxx.getSTATUS() != 0) {
			shjbxx_po.setSTATUS(shjbxx.getSTATUS());
		}
		// shjbxx_po.setOP_ITEM(getOpTime(param));
		// shjbxx_po.setOPID(getOpid(param) );
		getJpaDao().update(shjbxx_po);
	}

	private void xgfj(String CMS_WS_URL, String path_root, Map item) {
		String path = (String) item.get("PATH");
		Map TB_UFM_UPLOADFILE = (Map) getServiceInterface().doService(
				"SVC_UPLOADFILE", "GET_FILE_BY_ID", item.get("TPID"));
		if (path != null) {
			getServiceInterface().doWebService(CMS_WS_URL, "deleteFile", path);
		}
		path = path_root + "/" + item.get("TPID")
				+ TB_UFM_UPLOADFILE.get("EXT_NAME");
		getServiceInterface().doWebService(CMS_WS_URL, "releaseImage", path,
				TB_UFM_UPLOADFILE.get("FILE_BODY"));

		TB_SHGL_SHFJ shfj = getJpaDao().find(TB_SHGL_SHFJ.class,
				(String) item.get("ID"));
		shfj.setFILE_PATH(path);
		getJpaDao().update(shfj);
	}

	@Override
	public Map GET_RELEASE_INFO_DATA(String shid) {

		Map<String, Object> retParam = new HashMap<String, Object>();
		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put("SHID", shid);
		// 对象——商户基本信息
		Map JBXX = (Map) querySingleResult(
				"Q_SHGL_WS.GET_TB_SHGL_JBXX_BY_SHID", queryParam);
		retParam.put("OBJ_SHJBXX", JBXX);

		// 列表——商户扩展信息
		List kzsxList = getResultList("Q_SHGL_WS.GET_TB_SHGL_KZSXZ_BY_SHID",
				queryParam);
		retParam.put("LIST_KZXX", kzsxList);

		// 列表——商户门面图片
		List<Map> images = (List<Map>) getResultList(
				"Q_SHGL_WS.GET_TB_SHGL_SHMM_BY_SHID", queryParam);
		retParam.put("LIST_SHMM", images);

		// 列表—— 商品信息
		List<Map> spxxList = (List<Map>) getResultList(
				"Q_SHGL_WS.GET_TB_SHGL_SPXX_BY_SHID", queryParam);
		for (Map item : spxxList) {
			queryParam.put("SPID", item.get("ID"));
			// 列表——商品扩展信息
			item.put(
					"LIST_KZXX",
					getResultList("Q_SHGL_WS.GET_SPGL_SPXX_SXZ_BY_SPID",
							queryParam));
			// 列表——商品图片
			List<Map> sp_images = (List<Map>) getResultList(
					"Q_SHGL_WS.GET_SPGL_SPXX_SPTP_BY_SPID", queryParam);
			item.put("LIST_SPTP", sp_images);
		}
		retParam.put("LIST_SPXX", spxxList);

		// 列表—— 周边交通
		List zbjtList = getResultList("Q_SHGL_WS.GET_TB_SHGL_ZBJT_BY_SHID",
				queryParam);
		retParam.put("LIST_ZBJT", zbjtList);

		return retParam;
	}

	@Override
	public Map ADD_XXFBDL(Map param) {
		param.put("CREATE_TIME", getOpTime(param));
		Map ret = ADD(TB_SHGL_XXFBDL.class, param);
		getJpaDao().flush();
		return ret;
	}

	@Override
	public Map UPD_XXFBDL(Map param) {

		TB_SHGL_XXFBDL dto = ObjectUtils.mapToJavaBean(param,
				TB_SHGL_XXFBDL.class);
		TB_SHGL_XXFBDL po = getJpaDao().find(TB_SHGL_XXFBDL.class, dto.getID());
		ObjectUtils.copyJavaBeanToBean(dto, po, "ID", "SHID", "CREATE_TIME");

		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}
}
